package leetcode.editor.week.week310;

import java.util.HashMap;
import java.util.Map;

// 6176. 出现最频繁的偶数元素
// https://leetcode.cn/problems/most-frequent-even-element/
class Solution6176 {
    // hash table
    public int mostFrequentEven(int[] nums) {
        Map<Integer, Integer> map = new HashMap<>();
        for(int num : nums) {
            if(num % 2 == 0) map.put(num, map.getOrDefault(num, 0) + 1);
        }
        
        int max = 0;
        int res = -1;
        for(Map.Entry<Integer, Integer> entry : map.entrySet()) {
            if(entry.getValue() > max) {
                res = entry.getKey();
                max = entry.getValue();
            } else if(entry.getValue() == max) {
                res = Math.min(res, entry.getKey());
            }
        }
        
        return res;
    }
}
